from typing import List


class Solution:
    def rearrangeArray(self, nums: List[int]) -> List[int]:
        nums.sort()

        ans = []
        i1, i2 = 0, len(nums) - 1

        while i1 < i2:
            ans.append(nums[i1])
            ans.append(nums[i2])
            i1 += 1
            i2 -= 1

        if i1 == i2:
            ans.append(nums[i1])

        return ans


if __name__ == "__main__":
    print(Solution().rearrangeArray(nums=[1, 2, 3, 4, 5]))
    print(Solution().rearrangeArray(nums=[6, 2, 0, 9, 7]))

    # 自制用例
    print(Solution().rearrangeArray(nums=[1, 3, 4, 5, 6]))
